package com.task.web.controller.core;

import com.task.common.constant.ErrorCodeConstants;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.ClientAjaxResult;
import com.task.common.core.redis.RedisCache;
import com.task.core.domain.DeviceInfo;
import com.task.core.dto.request.ClientDataCompleteRequestDTO;
import com.task.core.dto.request.ClientMissionCompleteRequestDTO;
import com.task.core.dto.request.MissionGetRequestDTO;
import com.task.core.dto.response.DataOperationResponseDTO;
import com.task.core.dto.response.MissionOperationResponseDTO;
import com.task.core.service.ClientService;
import com.task.core.service.SysFileService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 迪迦.
 * @date 2024/12/24 15:21
 */
@RestController
@RequestMapping("/client")
public class ClientController extends BaseController {

  @Resource
  private ClientService clientService;

  @Resource
  private SysFileService fileService;

  @Resource
  private RedisCache redisCache;

  @GetMapping("/getDeviceByName")
  public ClientAjaxResult getDeviceByName(String deviceName, String authentication) {
    long startTime = System.currentTimeMillis();
    clientService.checkAuthentication(authentication);
    Map<String, String> map = new HashMap<>();
    DeviceInfo device = clientService.getDeviceByName(deviceName, authentication);
    map.put("deviceCode", device.getCode());
    map.put("deviceType", device.getDeviceType());
    map.put("deviceSerialNumber",device.getDeviceSeqNumber());
    long endTime = System.currentTimeMillis();
    String processTime = (endTime-startTime)+"毫秒";
    return ClientAjaxResult.success(map,processTime);
  }

  @GetMapping("/returnDeviceCompleteStatus")
  public ClientAjaxResult returnDeviceCompleteStatus(String deviceCode, String authentication) {
//    clientService.checkAuthentication(authentication);
    long startTime = System.currentTimeMillis();
    clientService.returnDeviceCompleteStatus(deviceCode);
    long endTime = System.currentTimeMillis();
    String processTime = (endTime-startTime)+"毫秒";
    return ClientAjaxResult.success("success",processTime);
  }

  @GetMapping("/getMissionByDeviceCode")
  public ClientAjaxResult getMissionByDeviceCode(MissionGetRequestDTO request, String authentication) {
//    clientService.checkAuthentication(authentication);
    long startTime = System.currentTimeMillis();
    MissionOperationResponseDTO missionByDeviceCode = clientService.getMissionByDeviceCode(request);
    if(ObjectUtils.isEmpty(missionByDeviceCode)){
      return ClientAjaxResult.error(ErrorCodeConstants.NO_CAN_USE_MISSION.getMsg());
    }
    long endTime = System.currentTimeMillis();
    String processTime = (endTime-startTime)+"毫秒";
    return ClientAjaxResult.success(missionByDeviceCode,processTime);
  }

  @GetMapping("/getDataByMissionSessionCode")
  public ClientAjaxResult getDataByMissionSessionCode(String missionSessionCode, String dataType, String authentication) {
//    clientService.checkAuthentication(authentication);
    long startTime = System.currentTimeMillis();
    DataOperationResponseDTO dataByMissionCode = clientService.getDataByMissionCode(missionSessionCode, dataType);
    long endTime = System.currentTimeMillis();
    String processTime = (endTime-startTime)+"毫秒";
    return ClientAjaxResult.success(dataByMissionCode,processTime);
  }

  @PostMapping("/returnDataCompleteStatus")
  public ClientAjaxResult returnDataCompleteStatus(@RequestBody ClientDataCompleteRequestDTO request) {
//    clientService.checkAuthentication(request.getAuthentication());
    long startTime = System.currentTimeMillis();
    boolean b = clientService.returnDataCompleteStatus(request.getDataSessionCode(),
        request.isSuccess());
    if (!b) {
      return ClientAjaxResult.error("处理异常,联系管理员");
    }
    long endTime = System.currentTimeMillis();
    String processTime = (endTime-startTime)+"毫秒";
    return ClientAjaxResult.success(endTime-startTime,"",processTime);
  }

  @PostMapping("/returnMissionCompleteStatus")
  public ClientAjaxResult returnMissionCompleteStatus(@RequestBody ClientMissionCompleteRequestDTO request) {
//    clientService.checkAuthentication(request.getAuthentication());
    long startTime = System.currentTimeMillis();
    boolean b = clientService.returnMissionCompleteStatus(request);
    if (!b) {
      return ClientAjaxResult.error("处理异常,联系管理员");
    }
    long endTime = System.currentTimeMillis();
    String processTime = (endTime-startTime)+"毫秒";
    return ClientAjaxResult.success(endTime-startTime,"",processTime);
  }

  @GetMapping("/fileOperation/{authentication}/{code}")
  public void download(@PathVariable String authentication, @PathVariable String code, HttpServletResponse response)
      throws UnsupportedEncodingException {
//    clientService.checkAuthentication(authentication);
    fileService.download(code, response);
  }

}
